package com.myapplication.interfaces;

import com.myapplication.callback.VerifyNumberCallback;
import com.myapplication.http.util.Util;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2016/5/20.
 */
public class VertifyNumberHttpConnect implements VerfiyNumberInter {

    @Override
    public void getVerfiyNumber(String phonenumber, final VerifyNumberCallback verifyNumberCallback) {
        RequestParams requestParams = new RequestParams(Util.UriVerfiyNumber);
        requestParams.addBodyParameter("username", phonenumber);
        requestParams.addBodyParameter("client", "android");
        requestParams.setConnectTimeout(10000);
        x.http().request(HttpMethod.POST,requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                verifyNumberCallback.onVerifyNumberCallbackSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                int code =-1;
                String errcode="";
                if (ex instanceof HttpException) {
                    code = ((HttpException) ex).getCode();
                    errcode = ((HttpException) ex).getErrorCode();
                }
                verifyNumberCallback.onVerifyNumberCallbackSuccess(code+errcode);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
